<?php

require_once dirname(__FILE__) . '/../../../bootstrap.unit.php';
require_once FORAGE_APP_PATH . '/engine/solr/SolrEngine.php';
require_once FORAGE_APP_PATH . '/behaviours/geo/GeoQueryBehaviour.php';

class ForageGeoQueryBehaviourTest extends ForageUnitTestCase
{
  private $query;
  private $engine;
  private $forage;

  public function setUp()
  {
    $this->query  = new ForageQuery();
    $this->query->addBehaviour(new ForageGeoQueryBehaviour());
    $this->engine = $this->getMock('ForageSolrEngine');
    $this->forage = new Forage($this->engine);
  }

  public function tearDown()
  {
    unset($this->engine, $this->forage);
  }

  public function testSetPoint()
  {
    $this->query->setPoint(51.2393, -0.1726);
    $this->assertEquals($this->query->latitude, 51.2393);
    $this->assertEquals($this->query->longitude, -0.1726);
  }

  public function testPointBadArgumentException()
  {
    try {
      $this->query->setPoint('foo', 123.0);
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertRegExp('/Expecting numeric values/', $e->getMessage());
    }
  }

  public function testPointOutOfLatitudeRange()
  {
    try {
      $this->query->setPoint(95, 123);
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertRegExp('/Invalid latitude/', $e->getMessage());
    }
    try {
      $this->query->setPoint(-95, 123);
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertRegExp('/Invalid latitude/', $e->getMessage());
    }
  }

  public function testPointOutOfLongitudeRange()
  {
    try {
      $this->query->setPoint(80, 200);
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertRegExp('/Invalid longitude/', $e->getMessage());
    }
    try {
      $this->query->setPoint(80, -200);
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertRegExp('/Invalid longitude/', $e->getMessage());
    }
  }

  public function testSetRadius()
  {
    $this->query->setRadius(1.0);
    $this->assertEquals($this->query->radius, 1.0);
  }

  public function testRadiusFailure()
  {
    try {
      $this->query->setRadius('foo');
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertRegExp('/Expecting a numeric/', $e->getMessage());
    }
  }
}
